0.1 Libraries

library(tidyverse)
## ── Attaching packages ───────────────────────────────────────────────────────────── tidyverse 1.3.0 ──
## ✓ ggplot2 3.3.2     ✓ purrr   0.3.4
## ✓ tibble  3.0.3     ✓ dplyr   1.0.2
## ✓ tidyr   1.1.2     ✓ stringr 1.4.0
## ✓ readr   1.3.1     ✓ forcats 0.5.0
## Warning: package 'tibble' was built under R version 4.0.2
## Warning: package 'tidyr' was built under R version 4.0.2
## Warning: package 'dplyr' was built under R version 4.0.2
## ── Conflicts ──────────────────────────────────────────────────────────────── tidyverse_conflicts() ──
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
library(here)
## Warning: package 'here' was built under R version 4.0.2
## here() starts at /Users/keegan.korthauer/Dropbox/Work/Teaching/Datajam2020/project_8

1.0 Read in Data

gcsc_dat <- read_csv(here("cleaned_gcsc_data.csv"))
## Parsed with column specification:
## cols(
##   .default = col_double(),
##   site_name = col_character(),
##   province = col_character(),
##   nearest_city = col_character(),
##   municipality = col_character(),
##   group_name_2017_onwards = col_character(),
##   group_type = col_character(),
##   site_shoreline_type = col_character(),
##   cleanup_date = col_datetime(format = ""),
##   date = col_date(format = ""),
##   time = col_time(format = ""),
##   unusual_items = col_character(),
##   `Site Name` = col_character()
## )
## See spec(...) for full column specifications.
ban_dat <- read_csv(here("ban_data.csv"))
## Parsed with column specification:
## cols(
##   Type = col_character(),
##   City = col_character(),
##   Province = col_character(),
##   `Effective Date` = col_character()
## )

2.0 Summary Plots

2.1 Types of plastic found by geographical location

plastics <- gcsc_dat %>% 
  pivot_longer(names_to = "plastic_type", 
               values_to = "number_collected", 
               cols = c(22:27, 30, 32:34, 37:39,42, 46 )) %>%
  mutate(plastic_type_new = ifelse(str_detect(plastic_type, "bottles"), "bottles", plastic_type))

plastics_group <-plastics %>% 
  group_by(year, province, plastic_type_new) %>% 
  summarise(total = sum(number_collected, na.rm = T))
## `summarise()` regrouping output by 'year', 'province' (override with `.groups` argument)
bar_plot_type <- ggplot(plastics_group, 
       aes(x=province,
           y=total, 
           group=plastic_type_new, 
           fill = plastic_type_new)) + 
  scale_y_log10()+
  geom_col() +
  labs(title="Amount of plastics by type and province")
bar_plot_type
## Warning: Transformation introduced infinite values in continuous y-axis
## Warning: Removed 86 rows containing missing values (geom_col).

ggsave(here("plastics_by_type_bar.png"),width = 10, height = 6)
## Warning: Transformation introduced infinite values in continuous y-axis

## Warning: Removed 86 rows containing missing values (geom_col).
plastics_group2 <-plastics %>% 
  group_by(year, province, plastic_type_new) %>% 
  summarise(total_kg = sum(kilograms,na.rm=T),
            item_kg=sum(number_collected, na.rm = T)/total_kg) %>%
    mutate(year_lab = as.character(year))
## `summarise()` regrouping output by 'year', 'province' (override with `.groups` argument)
plastics_group2
## # A tibble: 560 x 6
## # Groups:   year, province [40]
##     year province plastic_type_new                     total_kg item_kg year_lab
##    <dbl> <chr>    <chr>                                   <dbl>   <dbl> <chr>   
##  1  2017 AB       bags_plastic_plastic_bags_from_2017…     7793 0.210   2017    
##  2  2017 AB       bottle_caps_plastic_and_metal_2017_…     7793 0.391   2017    
##  3  2017 AB       bottles                                 15586 0.0660  2017    
##  4  2017 AB       buoys_floats_fishing_buoy_pot_or_tr…     7793 0.00385 2017    
##  5  2017 AB       cigarettes_cigarette_filters             7793 3.17    2017    
##  6  2017 AB       coffee_cups                              7793 0.0802  2017    
##  7  2017 AB       cups_and_plates_plastic_plastic_cup…     7793 0.0671  2017    
##  8  2017 AB       food_wrappers_containers                 7793 0.489   2017    
##  9  2017 AB       forks_knives_spoons_utensils_from_2…     7793 0.0595  2017    
## 10  2017 AB       other_plastic_foam_packaging_other_…     7793 0.213   2017    
## # … with 550 more rows
bar_plot_type2 <- ggplot(plastics_group2, 
       aes(x=province,
           y=total_kg  )) + 
  scale_y_log10()+
  geom_col() +
  labs(title="Kg of plastics by province")
bar_plot_type2
## Warning: Transformation introduced infinite values in continuous y-axis
## Warning: Removed 28 rows containing missing values (geom_col).

ggsave(here("plastics_bar_kg.png"),width = 10, height = 6)
## Warning: Transformation introduced infinite values in continuous y-axis

## Warning: Removed 28 rows containing missing values (geom_col).
bar_plot_type2 <- ggplot(plastics_group2, 
       aes(x=province,
           y=item_kg, 
           group=plastic_type_new, 
           fill = plastic_type_new)) + 
  geom_col() +
  labs(title="Type of plastic by kg collected and province")
bar_plot_type2
## Warning: Removed 28 rows containing missing values (position_stack).

ggsave(here("kg_plastics_by_type_bar.png"),width = 10, height = 6)
## Warning: Removed 28 rows containing missing values (position_stack).

2.2 Time plots (Roshni)

total_plastics_time <- plastics %>%
  group_by(year, province) %>%
  summarise(total = sum(number_collected, na.rm = T)) %>%
  mutate(year_lab = as.character(year))
## `summarise()` regrouping output by 'year' (override with `.groups` argument)
ggplot(total_plastics_time,
       aes(x=year_lab,
           y=total,
           group=province,
           colour=province)) +
 # facet_wrap(province ~.) +
  geom_line()+
  scale_y_log10()
## Warning: Transformation introduced infinite values in continuous y-axis

 # scale_colour_manual(values=myCol)
ggsave(here("total_plastics_time.jpeg"), width = 10, height = 6 )
## Warning: Transformation introduced infinite values in continuous y-axis
kg_plastics_time <- plastics %>%
  group_by(year, province) %>%
  summarise(total_kg = sum(kilograms, na.rm = T)) %>%
  mutate(year_lab = as.character(year))
## `summarise()` regrouping output by 'year' (override with `.groups` argument)
ggplot(kg_plastics_time,
       aes(x=year_lab,
           y=total_kg,
           group=province,
           colour=province)) +
  geom_line()+
  scale_y_log10()
## Warning: Transformation introduced infinite values in continuous y-axis

kg_plastics_time
## # A tibble: 40 x 4
## # Groups:   year [3]
##     year province total_kg year_lab
##    <dbl> <chr>       <dbl> <chr>   
##  1  2017 AB         116895 2017    
##  2  2017 BC         603615 2017    
##  3  2017 MB          13560 2017    
##  4  2017 NB          35610 2017    
##  5  2017 NL          14400 2017    
##  6  2017 NS          38925 2017    
##  7  2017 NT            735 2017    
##  8  2017 NU          37500 2017    
##  9  2017 ON         368115 2017    
## 10  2017 PE          23685 2017    
## # … with 30 more rows
ggsave(here("kg_plastics_time.jpeg"), width = 10, height = 6 )
## Warning: Transformation introduced infinite values in continuous y-axis
####Grouping by year & Province & type
plastics_group <-plastics %>%
  group_by(year, province, plastic_type_new) %>%
  summarise(total = sum(number_collected, na.rm = T)) %>%
  mutate(year_lab = as.character(year))
## `summarise()` regrouping output by 'year', 'province' (override with `.groups` argument)
prov_names <- unique(plastics_group$province)
for (i in seq_along(prov_names)) {
  chart <- subset(plastics_group, plastics_group$province == prov_names[i])
 plot <- ggplot(chart,
         aes(x=year_lab,
             y=total,
             group=plastic_type_new,
             colour=plastic_type_new)) +
    geom_line()+
    theme(legend.position = "right") +
   labs(title = paste("Total items by Type of Plastic for", prov_names[i]))+
   scale_y_log10()
 print(plot)
 ggsave( here(paste("Total items by Type of Plastic for", prov_names[i], ".jpeg")), width = 10, height = 6 )
  }

## Warning: Transformation introduced infinite values in continuous y-axis

## Warning: Transformation introduced infinite values in continuous y-axis

## Warning: Transformation introduced infinite values in continuous y-axis

## Warning: Transformation introduced infinite values in continuous y-axis

## Warning: Transformation introduced infinite values in continuous y-axis

## Warning: Transformation introduced infinite values in continuous y-axis

2.2 Time plots (Roshni) - adjusted for person*km

total_plastics_time <- plastics %>%
  group_by(year, province) %>%
  summarise(total = sum(number_collected, na.rm = T),
            person = sum(adults_2017_onwards_participant_count, na.rm=T),
            totkm = sum(kilometers, na.rm=T)) %>%
  mutate(amts_personkm=total/(person*totkm)) %>%
  mutate(year_lab = as.character(year))
## `summarise()` regrouping output by 'year' (override with `.groups` argument)
ggplot(total_plastics_time,
       aes(x=year_lab,
           y=amts_personkm,
           group=province,
           colour=province)) +
 # facet_wrap(province ~.) +
  geom_line()+
  scale_y_log10()
## Warning: Transformation introduced infinite values in continuous y-axis
## Warning: Removed 1 row(s) containing missing values (geom_path).

 # scale_colour_manual(values=myCol)
ggsave(here("total_plastics_time_adjusted.jpeg"), width = 10, height = 6 )
## Warning: Transformation introduced infinite values in continuous y-axis

## Warning: Removed 1 row(s) containing missing values (geom_path).
####Grouping by year & Province & type
plastics_group <-plastics %>%
  group_by(year, province, plastic_type_new) %>%
    summarise(total = sum(number_collected, na.rm = T),
            person = sum(adults_2017_onwards_participant_count, na.rm=T),
            totkm = sum(kilometers, na.rm=T)) %>%
  mutate(amts_personkm=total/(person*totkm)) %>%
  mutate(year_lab = as.character(year))
## `summarise()` regrouping output by 'year', 'province' (override with `.groups` argument)
prov_names <- unique(plastics_group$province)
for (i in seq_along(prov_names)) {
  chart <- subset(plastics_group, plastics_group$province == prov_names[i])
 plot <- ggplot(chart,
         aes(x=year_lab,
             y=amts_personkm,
             group=plastic_type_new,
             colour=plastic_type_new)) +
    geom_line()+
    theme(legend.position = "right") +
   labs(title = paste("Total items by Type of Plastic for", prov_names[i]))+
   scale_y_log10()
 print(plot)
 ggsave( here(paste("Total items by Type of Plastic for", prov_names[i], "_adjusted.jpeg")), width = 10, height = 6 )
  }

## Warning: Transformation introduced infinite values in continuous y-axis

## Warning: Transformation introduced infinite values in continuous y-axis

## Warning: Transformation introduced infinite values in continuous y-axis

## Warning: Transformation introduced infinite values in continuous y-axis

## Warning: Transformation introduced infinite values in continuous y-axis

## Warning: Transformation introduced infinite values in continuous y-axis

3.0 Rates

plastics_rates <- gcsc_dat %>%
  mutate(amts_person=kilograms/adults_2017_onwards_participant_count) %>%
  mutate(ppl_km=adults_2017_onwards_participant_count/kilometers) %>%
  mutate(amts_personkm=kilograms/(adults_2017_onwards_participant_count*kilometers)) %>%
  mutate(glass_bottle_kg=glass_beverage_bottles/kilograms) %>%
  mutate(year_lab = as.character(year))
mean_amts <- plastics_rates %>% group_by(year_lab, province) %>%
  summarize(mean_amts_person=mean(amts_person, na.rm = T)
            , mean(ppl_km,na.rm = T),mean(glass_bottle_kg,na.rm = T))
## `summarise()` regrouping output by 'year_lab' (override with `.groups` argument)
mean_amts
## # A tibble: 40 x 5
## # Groups:   year_lab [3]
##    year_lab province mean_amts_person `mean(ppl_km, na.r… `mean(glass_bottle_kg…
##    <chr>    <chr>               <dbl>               <dbl>                  <dbl>
##  1 2017     AB                   1.50                21.6                Inf    
##  2 2017     BC                   3.53                24.5                Inf    
##  3 2017     MB                   2.73                15.1                  0.264
##  4 2017     NB                   7.84                14.0                  0.176
##  5 2017     NL                   4.13                12.2                  0.749
##  6 2017     NS                   2.58                14.9                  0.208
##  7 2017     NT                   3.36                 3.8                  0.5  
##  8 2017     NU                   7.14               117.                 NaN    
##  9 2017     ON                   5.94               Inf                  Inf    
## 10 2017     PE                   2.58                25.9                Inf    
## # … with 30 more rows
plastics_rates %>% 
  ggplot(aes(x=province,y=amts_person,col=year_lab)) +
  geom_boxplot()+ 
  coord_cartesian(ylim=c(0,20))+
  labs(title="Amount per person per province over time")
## Warning: Removed 2115 rows containing non-finite values (stat_boxplot).

ggsave(here("amts_person_time.jpeg"))
## Saving 7 x 5 in image
## Warning: Removed 2115 rows containing non-finite values (stat_boxplot).
plastics_rates %>% 
  ggplot(aes(x=province,y=ppl_km,col=year_lab)) +
  geom_boxplot()+ 
   coord_cartesian(ylim=c(0,300))+
  labs(title="People per km per province over time")
## Warning: Removed 2345 rows containing non-finite values (stat_boxplot).

ggsave(here("ppl_km_time.jpeg"))
## Saving 7 x 5 in image
## Warning: Removed 2345 rows containing non-finite values (stat_boxplot).
plastics_rates %>% 
  ggplot(aes(x=province,y=amts_personkm,col=year_lab)) +
  geom_boxplot()+ 
  scale_y_log10()+
  labs(title="Amount per person*km per province over time")
## Warning: Transformation introduced infinite values in continuous y-axis
## Warning: Removed 2696 rows containing non-finite values (stat_boxplot).

ggsave(here("amts_ppl_km_time.jpeg"))
## Saving 7 x 5 in image
## Warning: Transformation introduced infinite values in continuous y-axis

## Warning: Removed 2696 rows containing non-finite values (stat_boxplot).

Ban data

knitr::include_graphics(c("bans.png","bans_province.png", "bans_year.png", "bans_year_prov.png"))